//
//  JFBOrderRefuseDS.m
//  ios-supplier
//
//  Created by feynmanyuan on 15/4/17.
//  Copyright (c) 2015年 feynmanyuan. All rights reserved.
//

#import "JFBRefuseOrderDS.h"
#import "JFBMerchandiseDS.h"

@implementation JFBRefuseOrderDS
- (instancetype)initWithAttributes:(NSDictionary *)attributes {
    self = [super initWithAttributes:attributes];
    if (!self) {
        return nil;
    }
    
    self.role = @"商户";
    self.reason = [[attributes objectForKey: @"refuse_reason"] isEqual: [NSNull null]] ? @"" : [attributes objectForKey: @"refuse_reason"];
    self.refundStatus = @"";
    return self;
}


- (void)buildWithMockAttributes {
    self.role = @"商户";
    self.reason = @"无货";
    self.refundStatus = @"退款中";
    
    
    self.ID = 12;
    self.orderCode = @"8267387867";
    self.address = @"都会国际3J";
    self.requireTime = @"30分钟内送达";
    self.payment = @"货到付款";
    self.paymentOrigin = 0;
    self.totalCount = 3;
    self.money = [NSDecimalNumber decimalNumberWithString:@"58.3"];
    self.createAt = @"2015-12-3 12:04";
    self.userName = @"hello world";
    self.userMobile = @"18696794082";
    self.staff = @"小piu~";
    self.staffMobile = @"18767654389";
    self.remark = @"姑娘，带包黄鹤楼";
    
    self.originMoney = [NSDecimalNumber decimalNumberWithString:@"80.0"];
    self.carriageMoney = [NSDecimalNumber decimalNumberWithString:@"5.0"];
    self.promotionMoney = [NSDecimalNumber decimalNumberWithString:@"16.7"];
    
    
    self.merchandiseList = [[NSMutableArray alloc] init];
    for (int i = 0; i <5; i++) {
        [self.merchandiseList addObject:[[JFBMerchandiseDS alloc] initWithAttributes:nil]];
    }
}

+ (NSURLSessionDataTask *) getOrderDetail:(void (^)(JFBRefuseOrderDS *order, NSError *error))block orderID:(NSUInteger)orderID {
    if (block) {
        block([[JFBRefuseOrderDS alloc] initWithAttributes:nil], nil);
    }
    return nil;
}

+ (NSURLSessionDataTask *)loadOrderList:(void (^)(NSArray *posts, NSError *error))block pager:(JFBPager*)pager{
    
    NSDictionary * parameter = @{@"so_status":[NSNumber numberWithInt:kOrderStatusRefuse]};
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/so/list.json" parameters:parameter success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSLog(@"%@", JSON);
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            NSMutableArray *result = [@[] mutableCopy];
            NSArray *response = [JSON valueForKeyPath:@"data"];
            for( NSDictionary *order in response ) {
                [result addObject:[[JFBRefuseOrderDS alloc] initWithAttributes:order]];
            }
            NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"updateAt" ascending:NO];
            [result sortUsingDescriptors:[NSArray arrayWithObject:sortDescriptor]];
            block([NSArray arrayWithArray:result], nil);
        } else {
            block(nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(nil, error);
    }];
    return nil;
}

+ (NSURLSessionDataTask *)refuse:(void (^)(BOOL isSuccess, JFBRefuseOrderDS *order, NSError *error))block orderID:(NSUInteger)orderID  reason:(NSString*)reason{
    
    NSDictionary *parameter = @{@"so_id":[NSNumber numberWithInteger:orderID], @"cancel_content":reason};
    
    return [[JFBAPIClient sharedClient:true] GET:@"/shop/so/refuse.json" parameters:parameter success:^(NSURLSessionDataTask * __unused task, id JSON) {
        NSObject * error = [JSON valueForKey:@"error"];
        if( error == [NSNull null] ) {
            block(true, nil, nil);
        } else {
            block(false, nil, nil);
        }
    } failure:^(NSURLSessionDataTask *__unused task, NSError *error) {
        block(false, nil, error);
    }];
    return nil;
}

+ (NSURLSessionDataTask *)refund:(void (^)(BOOL isSuccess, JFBOrderDS *order, NSString *message, NSError *error))block orderID:(NSUInteger)orderID {
    if (block) {
        block(true, [[JFBRefuseOrderDS alloc] initWithAttributes:nil], @"退款申请提交成功", nil);
    }
    return nil;
}

@end
